package com.example.shop.api;

import com.example.shop.base.ApiBaseController;
import com.example.shop.base.Result;
import com.example.shop.commodity.entity.ShopCategorys;
import com.example.shop.commodity.entity.ShopCommodity;
import com.example.shop.commodity.service.ShopCategorysService;
import com.example.shop.commodity.service.ShopCommodityService;
import com.example.shop.common.annotation.LoginIgnore;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/shop/commodity")
public class ApiShopCommodityController extends ApiBaseController {

    @Autowired
    private ShopCommodityService commodityService;
    @Autowired
    private ShopCategorysService categorysService;

    @GetMapping("/search")
    public Result search(ShopCommodity shopCommodity) {
        PageInfo<ShopCommodity> pageInfo = commodityService.search(shopCommodity);
        Map<String, Object> map = null;
        List<Map<String, Object>> list = new ArrayList<>();
        ShopCategorys category = null;
        for (ShopCommodity commodity : pageInfo.getList()) {
            map = new HashMap<>();
            map.put("id", commodity.getId());
            map.put("picture", picture_url + commodity.getPictures());
            category = categorysService.get(commodity.getCid());
            if (null != category) {
                map.put("categoryName", category.getName());
            }
            map.put("name", commodity.getName());
            map.put("price", commodity.getPrice());
            map.put("desc", commodity.getDescs());
            map.put("author", commodity.getAuthor());
            map.put("stock", commodity.getStock());
            list.add(map);
        }
        Map<String, Object> map1 = new HashMap<>();
        map1.put("isHasNextPage", pageInfo.isHasNextPage());
        map1.put("data", list);
        return Result.success(map1);
    }

    /**
     * 首页展示
     *
     * @param shopCommodity
     * @return
     */
    @GetMapping("/hs")
    @LoginIgnore
    public Result hs(ShopCommodity shopCommodity) {
        shopCommodity.setStatus("0");
        List<ShopCommodity> shopCommodityList = commodityService.findList(shopCommodity);
        Map<String, Object> map = null;
        ShopCategorys category = null;
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShopCommodity commodity : shopCommodityList) {
            map = new HashMap<>();
            map.put("id", commodity.getId());
            map.put("picture", picture_url + commodity.getPictures());
            category = categorysService.get(commodity.getCid());
            if (null != category) {
                map.put("categoryName", category.getName());
            }
            map.put("name", commodity.getName());
            map.put("price", commodity.getPrice());
            map.put("desc", commodity.getDescs());
            map.put("author", commodity.getAuthor());
            map.put("stock", commodity.getStock());
            list.add(map);
        }
        return Result.success(list);
    }

    /**
     * 分类列表
     *
     * @param shopCommodity
     * @return
     */
    @GetMapping("/list")
    @LoginIgnore
    public Result list(ShopCommodity shopCommodity) {
        shopCommodity.setStatus("0");
        PageInfo<ShopCommodity> pageInfo = commodityService.findPage(shopCommodity);
        Map<String, Object> map = null;
        List<Map<String, Object>> list = new ArrayList<>();
        ShopCategorys category = null;
        for (ShopCommodity commodity : pageInfo.getList()) {
            map = new HashMap<>();
            map.put("id", commodity.getId());
            map.put("picture", picture_url + commodity.getPictures());
            category = categorysService.get(commodity.getCid());
            if (null != category) {
                map.put("categoryName", category.getName());
            }
            map.put("name", commodity.getName());
            map.put("price", commodity.getPrice());
            map.put("desc", commodity.getDescs());
            map.put("author", commodity.getAuthor());
            map.put("stock", commodity.getStock());
            list.add(map);
        }
        Map<String, Object> map1 = new HashMap<>();
        map1.put("isHasNextPage", pageInfo.isHasNextPage());
        map1.put("data", list);
        return Result.success(map1);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @LoginIgnore
    public Result get(@PathVariable("id") String id) {
        ShopCommodity commodity = commodityService.get(id);
        if (null != commodity) {
            commodity.setPictures(picture_url + commodity.getPictures());
            ShopCategorys category = categorysService.get(commodity.getCid());
            if (null != category) {
                commodity.setCategoryName(category.getName());
            }
        }

        return Result.success(commodity);
    }
}
